﻿/**
 * 实例化angular 并初始化
 * */

define(['angularAMD','angular-route','angular-touch','util-module','platformRoute-module','customRoute-module'], function(angularAMD){
	
	//angular模块对象
	var app = angular.module('ngreq-app', ['ngRoute','ngTouch','utilModule','platformRoutemModule','customRoutemModule']);
	
	//初始配置参数
	app.config(function($routeProvider,$locationProvider,$platformRouteProvider,$customRouteProvider){
		
		var platformRoute = $platformRouteProvider.getPlatformRouteList();
		var customRoute	= $customRouteProvider.getCustomRouteList();
		
		if(!angular.isUndefined(platformRoute) && platformRoute.length > 0) {
			//生成平台angular路由路径
			for(var i = 0; i < platformRoute.length; i++) {
				$routeProvider
					.when(platformRoute[i].path, angularAMD.route({
						templateUrl		: platformRoute[i].templateUrl, 
						controller		: platformRoute[i].controller, 
						controllerUrl	: platformRoute[i].controllerUrl, 
						title			: platformRoute[i].title,
						isAuth			: platformRoute[i].isAuth
					}))
			}
		}
		
		if(!angular.isUndefined(customRoute) && customRoute.length > 0) {
			//生成定制angular路由路径
			for(var i = 0; i < customRoute.length; i++) {
				$routeProvider
					.when(customRoute[i].path, angularAMD.route({
						templateUrl		: customRoute[i].templateUrl, 
						controller		: customRoute[i].controller, 
						controllerUrl	: customRoute[i].controllerUrl, 
						title			: customRoute[i].title,
						isAuth			: customRoute[i].isAuth
					}))
			}
		}

		$routeProvider.when('/error', angularAMD.route({
			templateUrl: 'views/error.html',				
			title: '错误页面',
			isAuth: false
		}))
		.otherwise({redirectTo: '/error'});
	});
	
	//全局运行事件
	app.run(function($rootScope,$routeParams,$location,$http,layer,channel){		
		
		$rootScope.$on('$routeChangeStart', function(event, next){
			//加载等待图标开启
			$rootScope.loading = true;
			//是否已注册入口
			if(sessionStorage.getItem('isRegChannel') == null) {
				//获取当前入口
				if(angular.isUndefined(next.params.channel)){
					next.params = layer.getParameters(location.href)
				}
				var _channel = next.params.channel;

				if(angular.isUndefined(_channel)){						
					_channel = sessionStorage.getItem("channel");
				}
				var _url = encodeURIComponent(location.href.split("#")[0]);		
				channel.registerChannel({'channel':_channel,'url':_url},function(data){
					sessionStorage.setItem('isRegChannel',true);
					sessionStorage.setItem('channel',_channel);
					sessionStorage.setItem('appid',data.appId);
					//配置微信js
					layer.wxConfig(data);
					if(next.$$route.isAuth == "true") {
						//签权
						channel.wechatSign(next.$$route.isAuth,next.params.sid);
					}
				});		
			} else {
				if(!angular.isUndefined(next.$$route)) {
					if(next.$$route.isAuth == "true") {
						//签权
						channel.wechatSign(next.$$route.isAuth,next.params.sid);
					}
				}  
			}			
		}); 
		
		$rootScope.$on('$routeChangeSuccess', function(event, next){
			// unbind scroll
			$(window).unbind('scroll');
			$(window).scrollTop(0);
			//关闭论坛发帖层
			if(!angular.isUndefined($(".l-g-message_warp"))) {
				$(".l-g-message_warp").hide();
				$("#l-import2-shade").hide();	
			}	
			//个人中心导航栏显示的文字
			if(!angular.isUndefined(next.$$route)) {
				$rootScope.pc_title = next.$$route.title;
			}
			if(sessionStorage.getItem('microMartHomeUrl') != null) {
				//微商城拖拽按钮只在微商城页面中显示
				if($location.url().indexOf('microMart') != -1) {
					$(".g-dragdrop").show();
				} else {
					$(".g-dragdrop").hide();
					$("#dragdrop_l_shade").hide();
					//删除遮罩层
					if(!angular.isUndefined($("#dragdrop_l_shade1"))) {
						$("#dragdrop_l_shade1").remove();
					}
				}
			}
			//找到访问路径,如果是经纪人页面则隐藏右上角分享功能
			if($location.url().indexOf('widemarking') != -1){
				wx.hideOptionMenu();
			} else {
				wx.showOptionMenu();
			}
			//加载等待图标关闭
			$rootScope.loading = false;
		});
		
	});		
	
	//自定义当一个集合渲染完成后执行的事件，
	app.directive('onImgRenderDirective', function ($timeout) {  
		return {    
			restrict: 'A',    
			link: function(scope, element, attr) {      
				if (scope.$last === true) {        
					$timeout(function() {          
						scope.$emit('ngRepeatLoad',element);        
					});      
				}    
			}  
		};
	});
	//主要用于绑定成功后获取绑定数据指令
	app.directive('onHtmlBindDirective', function ($timeout) {  
		return {    
			restrict: 'A',    
			link: function(scope, element, attr) {				      
				$timeout(function() {          
					scope.$emit('ngHtmlBindLoad');        
				},1000);      				    
			}  
		};
	});
	
	angularAMD.bootstrap(app);
		
	return app;
	
});

